import { findActivity, getTime, addActivity, deleteActivity, updateActivityStatus,
  findActivityDetailList, findActivityRebateList, editActivityDetail,
  searchMaintainOrder, cancelMaintainOrder, searchSysMaintainRuler,
  addMaintenanceruler, searchBrandbylogin, getSeries,
  updateMaintenanceRuler, deleteMaintenanceRuler, searchDealerbylogin } from '../services/maintenance';

export default {
  namespace: 'maintenance',
  state: {
    response: {
      list: [],
      pagination: {},
    },
    loading: false,
    tList: [],
    detailList: [],
    rebateList: [],
    orderList: [],
    ruleList: [],
    brandList: [],
    seriesList: [],
    dealerList: [],
  },
  effects: {
    *addActivity({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(addActivity, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *publishActivity({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(updateActivityStatus, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *delActivity({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(deleteActivity, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *updateActivity({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(editActivityDetail, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *fetchActivity({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(findActivity, payload);
      yield put({
        type: 'appendList',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *fetchActivityDetail({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(findActivityDetailList, payload);
      yield put({
        type: 'dList',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *fetchRebate({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(findActivityRebateList, payload);
      yield put({
        type: 'rList',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *fetchTime({ callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(getTime);
      yield put({
        type: 'timeList',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *fetchMaintainOrder({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(searchMaintainOrder, payload);
      yield put({
        type: 'oList',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *cancel({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(cancelMaintainOrder, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *fetchSysMaintainRuler({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(searchSysMaintainRuler, payload);
      yield put({
        type: 'srList',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *addSysMaintainRuler({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(addMaintenanceruler, payload);
      yield put({
        type: 'saveRule',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *updateMaintainRuler({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(updateMaintenanceRuler, payload);
      yield put({
        type: 'saveRule',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *delMaintainRuler({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(deleteMaintenanceRuler, payload);
      yield put({
        type: 'saveRule',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *fetchBrand({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(searchBrandbylogin, payload);
      yield put({
        type: 'bList',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *fetchDealer({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(searchDealerbylogin, payload);
      yield put({
        type: 'deaList',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *fetchSeries({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(getSeries, payload);
      yield put({
        type: 'serList',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    appendList(state, action) {
      return {
        ...state,
        response: action.payload,
      };
    },
    dList(state, action) {
      return {
        ...state,
        detailList: action.payload,
      };
    },
    rList(state, action) {
      return {
        ...state,
        rebateList: action.payload,
      };
    },
    oList(state, action) {
      return {
        ...state,
        orderList: action.payload,
      };
    },
    timeList(state, action) {
      return {
        ...state,
        tList: action.payload,
      };
    },
    srList(state, action) {
      return {
        ...state,
        ruleList: action.payload,
      };
    },
    saveRule(state, action) {
      return {
        ...state,
        result: action.payload,
      };
    },
    bList(state, action) {
      return {
        ...state,
        brandList: action.payload,
      };
    },
    deaList(state, action) {
      return {
        ...state,
        dealerList: action.payload,
      };
    },
    serList(state, action) {
      return {
        ...state,
        seriesList: action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
  },
};
